package com.bjc.sdx.activity;
import android.Manifest;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import com.bjc.sdx.R;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ContentProviderActivity extends AppCompatActivity {

    private ListView lv_main_list;
    private SimpleAdapter sa;
    private List<Map<String, Object>> data;
    private final String TAG = "ContentProvider";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        //得到ListView
        lv_main_list = (ListView) findViewById(R.id.lv_main_list);

        data =new ArrayList<Map<String,Object>>();
        //配置适配置器S
        sa = new SimpleAdapter(this,data,android.R.layout.simple_list_item_2,new String[]{"names","message"},new int[]{android.R.id
                .text1,android.R.id.text2});
        lv_main_list.setAdapter(sa);
        Button button = findViewById(R.id.dx);
        checkPermission(this);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                readSMS(view);
            }
        });
    }
    public void readSMS(View view) {
        //读取所有短信
        Log.d(TAG, "readSMS: 读取所有短信");
        Uri uri=Uri.parse("content://sms/");
        ContentResolver resolver = getContentResolver();
        Cursor cursor = resolver.query(uri, new String[]{"_id", "address", "body", "date", "type"},
                null, null, null);
        if(cursor!=null&&cursor.getCount()>0){
            int _id;
            String address;
            String body;
            String date;
            int type;
            while (cursor.moveToNext()){
                Map<String, Object> map=new HashMap<String, Object>();
                _id=cursor.getInt(0);
                address=cursor.getString(1);
                body=cursor.getString(2);
                date=cursor.getString(3);
                type=cursor.getInt(4);
                map.put("names",body);

                Log.i("test","_id="+_id+" address="+address+" body="+body+" date="+date+" type="+type);
                data.add(map);
                //通知适配器发生改变
                sa.notifyDataSetChanged();
            }
        }
    }


    //android6.0之后要动态获取权限
    private void checkPermission(Activity activity) {
        // Storage Permissions
        final int REQUEST_EXTERNAL_STORAGE = 1;
        String[] PERMISSIONS_STORAGE = {
                Manifest.permission.READ_SMS,
        };

        try {
            //检测是否有读短信箱的权限
            int permission = ActivityCompat.checkSelfPermission(ContentProviderActivity.this,
                    "android.permission.READ_SMS");
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // 没有读短信箱的权限，去申请写的权限，会弹出对话框
                ActivityCompat.requestPermissions(ContentProviderActivity.this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
